package maa.action.all;

import maa.action.Action;
import maa.action.ActionException;
import maa.action.ActionResult;
import maa.dao.DAOCommand;
import maa.dao.DAOException;
import maa.dao.DAOFactory;
import maa.dao.DAOManager;
import maa.dao.h2.UserDAOimpl;
import maa.database.ConnectionPool;
import maa.entity.Role;
import maa.entity.User;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Login implements Action {
    @Override
    public ActionResult execute(final HttpServletRequest request, HttpServletResponse response) throws Exception {
        final String email = request.getParameter("email");
        final String password = encryptPassword(request.getParameter("password"));
        ActionResult actionResult = new ActionResult();
        final HttpSession session = request.getSession();
        if (email == null || email.isEmpty()) {
            request.setAttribute("no_email", "Email field is empty!");
            actionResult.setResult("login");
            return actionResult;
        }
        if (password == null || password.isEmpty()) {
            request.setAttribute("em", email);
            request.setAttribute("no_password", "Password field is empty!");
            actionResult.setResult("login");
            return actionResult;
        }
        ConnectionPool connectionPool = ConnectionPool.getInstance();
        DAOManager daoManager = DAOFactory.createDAOManager(connectionPool.getConnection());
        DAOCommand command = new DAOCommand() {
            @Override
            public Object execute(DAOManager daoManager) throws ActionException {
                UserDAOimpl userDAO = daoManager.getUserDAO();
                User user = null;
                try {
                    user = userDAO.findByEmailAndPassword(email, password);
                } catch (DAOException e) {
                    throw new ActionException("Exception in userDAO.findByEmailAndPassword(email, password)");
                }
                return user;
            }
        };
        User user = (User) daoManager.executeAndClose(command);
        if (user.getName() == null) {
            request.setAttribute("input_error", "Incorrect email/password combination. Please try again!");
            request.setAttribute("em", email);
            actionResult.setResult("login");
            return actionResult;
        }
        if (user.getRole() == Role.ADMIN) {
            session.setAttribute("user", user);
            actionResult.setResult("admin-cabinet");
            actionResult.setRedirected(true);
            return actionResult;
        }
        if (user.getRole() == Role.CLIENT || user.getRole() == Role.MENTOR) {
            session.setAttribute("user", user);
            actionResult.setResult("account");
            actionResult.setRedirected(true);
            return actionResult;
        }
        return actionResult;
    }

    private static String encryptPassword(String password) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        MessageDigest crypt = MessageDigest.getInstance("SHA-1");
        crypt.reset();
        crypt.update(password.getBytes("UTF-8"));

        return new BigInteger(1, crypt.digest()).toString(16);

    }
}

